/* Copyright Andy Mitchell 2007. No warranties */

/*
If actions happen in rapid succession; process only the first one (and execute after timeout).
*/

function MultiToOne(timeDelay){
	this._timeDelay = timeDelay || 500;
	
	this._inProgress = {};	// this._inProgress[key] = {date, function}
};

MultiToOne.prototype.process = function(key, f){
	if( this._inProgress[key] ) return;
	
	this._inProgress[key] = {date: (new Date()).valueOf(), callback: f};
	setTimeout( GIPROTOTYPE.bind(this, function(){this._handle(key)}), this._timeDelay );
};

MultiToOne.prototype._handle = function(key){
	if( !this._inProgress[key] ) return;
	if( this._inProgress[key].callback ) this._inProgress[key].callback();
	this._inProgress[key] = null;
	delete this._inProgress[key];
};